package com.ljc.controller;

import com.auth0.jwt.interfaces.DecodedJWT;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ljc.dto.Result;
import com.ljc.entity.DeviceContract;
import com.ljc.service.DeviceContractService;
import com.ljc.service.DeviceService;
import com.ljc.util.JWTUtil;
import com.ljc.vo.DeviceContractSearch;
import com.sun.org.apache.regexp.internal.RE;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import sun.rmi.runtime.Log;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@ResponseBody
@CrossOrigin
@RequestMapping("deviceContract")
@Slf4j
public class DeviceContractController {
    @Autowired
    private DeviceContractService deviceContractService;


    @RequestMapping("add")
    public Result add(HttpServletRequest request, @RequestBody DeviceContract deviceContract){
        Result result = new Result();
        String token = request.getHeader("accessToken");
        DecodedJWT decodedJWT = JWTUtil.getTokenInfo(token);
        String id = decodedJWT.getClaim("id").asString();
        deviceContract.setUserId(Integer.valueOf(id));
        log.info("device contract: [{}]",deviceContract);
        try {
                deviceContractService.add(deviceContract);
                result.setStatus(1).setMsg("增加成功");
        }catch (Exception e){
            e.printStackTrace();
            result.setStatus(0).setMsg(e.getMessage());
        }
        return result;
    }
    @RequestMapping("searchByUser")
    public Result searchByUser(HttpServletRequest request, @RequestBody DeviceContractSearch search){
        Result result = new Result();
        String token = request.getHeader("accessToken");
        DecodedJWT decodedJWT = JWTUtil.getTokenInfo(token);
        String id = decodedJWT.getClaim("id").asString();
        search.setUserId(Integer.valueOf(id));
        log.info("device contract: [{}]",search);
        PageHelper.startPage(search.getPageNum(),search.getPageSize());
        List<DeviceContract> deviceContracts = deviceContractService.searchByUser(search);
        PageInfo<DeviceContract> deviceContractPageInfo = new PageInfo<>(deviceContracts);
        result.setStatus(1).setMsg("根据用户查询成功").setData(deviceContractPageInfo);
        return result;
    }
    @RequestMapping("getOne")
    public Result getOne(@RequestParam int id){
        Result result = new Result();
        DeviceContract one = deviceContractService.findOne(id);
        result.setData(one).setStatus(1).setMsg("根据id查找成功");
        return  result;
    }
    @RequestMapping("update")
    public Result update(@RequestBody DeviceContract deviceContract){
        Result result = new Result();
        try {
            deviceContractService.update(deviceContract);
            result.setMsg("修改成功").setStatus(1);
        }catch (Exception e){
            e.printStackTrace();
            result.setStatus(0).setMsg(e.getMessage());
        }
        return result;
    }
    @RequestMapping("delete")
    public  Result delete(@RequestParam int id ){
        Result result = new Result();
        try {
            deviceContractService.delete(id);
            result.setStatus(1).setMsg("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            result.setStatus(0).setMsg(e.getMessage());

        }
        return result;
    }
    @RequestMapping("searchByAdmit")
    public Result searchByAdmit(HttpServletRequest request,@RequestBody DeviceContractSearch search){
        Result result = new Result();
        String token = request.getHeader("accessToken");
        DecodedJWT decodedJWT = JWTUtil.getTokenInfo(token);
        String id = decodedJWT.getClaim("id").asString();
        search.setAdmitId(Integer.valueOf(id));
        PageHelper.startPage(search.getPageNum(),search.getPageSize());
        List<DeviceContract> deviceContracts = deviceContractService.searchByAdmit(search);
        PageInfo<DeviceContract> deviceContractPageInfo = new PageInfo<>(deviceContracts);
        result.setStatus(1).setMsg("根据管理员Id查询设备合同成功").setData(deviceContractPageInfo);
        return  result;
    }
}
